//
//  amqp_buffer.h
//  Amqp_test
//
//  Created by HNXF on 2022/7/27.
//

#ifndef amqp_buffer_h
#define amqp_buffer_h

class amqp_buffer {
    
public:
    amqp_buffer(size_t size)
        : data_(size, 0)
    , use_(0) {
        
    }
    
    size_t write(const char * data, size_t size) {
        if (use_ == data_.size()) {
            return 0;
        }
        
        const size_t length = (size + use_);
        size_t write = length < data_.size() ? size : (data_.size() - use_);
        memcpy(data_.data() + use_, data, write);
        use_ += write;
        return write;
    }
    
    void drain()
    {
        use_ = 0;
    }
    
    size_t available() const
    {
        return use_;
    }
    
    const char* data() const
    {
        return data_.data();
    }
    
    void shl(size_t count)
    {
        if (count < use_) {
            const size_t diff = use_ - count;
            std::memmove(data_.data(), data_.data() + count, diff);
            use_ = use_ - count;
        }
    }

private:
    std::vector<char> data_;
    size_t use_;
};
#endif /* amqp_buffer_h */
